/*
 * @Author: lokei
 * @Date: 2022-10-27 08:32:01
 * @LastEditors: lokei
 * @LastEditTime: 2022-10-27 19:54:00
 * @Description: 
 */
package cn.lokei.task.handler.iot.pc;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.lokei.task.entity.iot.mqtt.MyMqttClient;
import cn.lokei.task.entity.job.LockPCJob;

@Component
public class PCHandler {

    @Autowired
    SchedulerFactoryBean quartzScheduler;

    @Autowired
    Map<String, MyMqttClient> datewayClientMap;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void handler(MapRecord<String, String, String> record) {
        String pid = record.getValue().get("pid");
        String gateway_identity = record.getValue().get("gateway_identity");
        String topic = record.getValue().get("topic");
        String device_identity = record.getValue().get("device_identity");
        MyMqttClient mqttClient = datewayClientMap
                .get((!pid.equals("") ? pid + ":" : "") + gateway_identity + "_cloud");
        if (mqttClient != null) {
            JSONObject msg = new JSONObject();
            msg.put("msg", "1");
            mqttClient.publishMessage(topic + "_" + device_identity, msg.toJSONString(), 1);
        }
        if (topic.equals("pc_unlock")) {
            String lock_hour_str = record.getValue().get("lock_hour");
            if (lock_hour_str != null && !lock_hour_str.equals("")) {
                Integer lock_hour = Integer.valueOf(lock_hour_str);
                if (lock_hour > 0) {
                    Date date = new Date();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(Calendar.HOUR_OF_DAY, lock_hour);
                    JobDataMap map = new JobDataMap();
                    map.put("mqttClient", mqttClient);
                    map.put("redisTemplate", redisTemplate);
                    map.put("pid", pid);
                    map.put("device_identity", device_identity);
                    JobDetail jobDetail = JobBuilder.newJob(LockPCJob.class)
                            .withIdentity("pc_lock_" + device_identity, "pcLockGroup")
                            // JobDataMap可以给任务execute传递参数
                            .usingJobData(map)
                            .storeDurably()
                            .build();
                    Trigger trigger = TriggerBuilder.newTrigger()
                            .forJob(jobDetail)
                            .withIdentity("pc_lock_" + device_identity, "pcLockGroup")
                            .startNow()
                            // .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(20))
                            .withSchedule(CronScheduleBuilder.cronSchedule(calendar.get(Calendar.SECOND) + " "
                                    + calendar.get(Calendar.MINUTE) + " " + calendar.get(Calendar.HOUR_OF_DAY) + " "
                                    + calendar.get(Calendar.DAY_OF_MONTH) + " " + (calendar.get(Calendar.MONTH) + 1)
                                    + " ? " + calendar.get(Calendar.YEAR)))
                            .build();
                    try {
                        quartzScheduler.getScheduler().scheduleJob(jobDetail, trigger);
                    } catch (SchedulerException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
